<template>
    <view class="container">
        <oreder-header :placeholder="'输入销售单号/退货单号'" @searchFn="searchFn" @doFilter="doFilter"></oreder-header>
        <tab-bar :tabs="items" nobg="nobg" @clickTab="clickTab" :tabIndex="tabIndex"></tab-bar>
        <view class="main">
            <swiper
                class="swiper product-main-list"
                :style="{ height: listHeight }"
                :current="tabIndex"
                @change="swiperChange">
                <swiper-item v-for="(swiperItem, swiperIndex) in items" :key="swiperIndex">
                    <order-list
                        v-if="loadOk"
                        :ref="'productList_' + swiperIndex"
                        :height="listHeight"
                        :state="swiperItem.state"
                        :requestData="requestData"></order-list>
                </swiper-item>
            </swiper>
        </view>
    </view>
</template>

<script>
import { mapGetters } from "vuex"
import orederHeader from "@/pages_supplier/components/order/order-header.vue"
import orderList from "@/pages_supplier/components/returnOrder/order-list.vue"
import tabBar from "@/pages_supplier/components/common/tab-bar.vue"
import storage from "@/uni-sub-base/utils/storage.js"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
export default {
    components: {
        orederHeader,
        orderList,
        tabBar,
    },
    data() {
        return {
            optEmpty: {
                icon: "/static/img/empty.png", // 图标路径 (建议放入static目录, 如 /static/img/mescroll-empty.png )
                tip: "~ 暂无数据 ~", // 空提示
            },
            isloadding: false,
            tabIndex: 0,
            items: [
                {
                    state: "-1",
                    title: "全部",
                },
                {
                    state: "0",
                    title: "待审核",
                },
                {
                    state: "3",
                    title: "退货中",
                },
                {
                    state: "1",
                    title: "待退货",
                },
                {
                    state: "7",
                    title: "已退货",
                },
                {
                    state: "2",
                    title: "已取消",
                },
                {
                    state: "8",
                    title: "已驳回",
                },
            ],
            listHeight: "", //list所需高度
            sublistHeight: "", //list所需高度 待接收下面有按钮
            loadOk: false,
            searchKey: "",
            requestData: {
                searchKey: "",
                startTime: "",
                endTime: "",
                state: "",
            },
        }
    },
    watch: {
        //监听更新
        getUpReturnOrderView(e) {
            this.refresh()
        },
    },
    computed: {
        ...mapGetters(["getUpReturnOrderView"]),
    },
    onLoad(e) {
        if (e.tabIndex) {
            this.tabIndex = e.tabIndex
        }
        if (e.OrderNo) {
            this.requestData.buyOrderId = +e.OrderNo
        }
    },
    onShow() {
        //判断是要切到哪个tab
        let orderType = storage.getCache("orderType")
        if (orderType) {
            for (var i = 0; i < this.items.length; i++) {
                let item = this.items[i]
                if (item.state == orderType) {
                    this.tabIndex = i
                }
            }
            storage.removeCache("orderType")
        }
        let miniProgramState = storage.getCache("miniProgram")
        if (miniProgramState) {
            this.refresh()
            storage.removeCache("miniProgram")
        }
    },
    mounted() {
        this.getListHeight()
    },
    methods: {
        getListHeight(element) {
            //获取商品高度
            getElementHeight(this, ".main", (e) => {
                this.listHeight = e + "px"
                this.loadOk = true
            })
        },
        swiperChange(e) {
            let tabIndex = e.detail.current
            this.clickTab(tabIndex)
        },
        //tab切换
        clickTab(index) {
            if (this.tabIndex != index) {
                this.tabIndex = index
            }
        },
        //搜索
        searchFn(searchKey) {
            this.requestData.searchKey = searchKey
            this.refresh()
        },
        //筛选
        doFilter(data) {
            this.requestData.startTime = data.startDate ? data.startDate + " 00:00:00" : ""
            this.requestData.endTime = data.endDate ? data.endDate + " 23:59:59" : ""
            this.refresh()
        },
        refresh() {
            this.$refs.productList_0[0].refresh(this.requestData)
            this.$refs.productList_1[0].refresh(this.requestData)
            this.$refs.productList_2[0].refresh(this.requestData)
            this.$refs.productList_3[0].refresh(this.requestData)
            this.$refs.productList_4[0].refresh(this.requestData)
            this.$refs.productList_5[0].refresh(this.requestData)
            this.$refs.productList_6[0].refresh(this.requestData)
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    display: flex;
    flex-direction: column;
}
</style>
